package flc.ast.fragment;

import android.app.Dialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.FragmentRecognizeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RecognizeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RecognizeFragment extends BaseNoModelFragment<FragmentRecognizeBinding> {

    private List<MyRecordBean> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;
    private Dialog myDeleteDialog;


    @Override
    protected int onCreate() {
        return R.layout.fragment_recognize;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivTakeVideo.setOnClickListener(this);
        mDataBinding.ivRecognizeCar.setOnClickListener(this);
        mDataBinding.ivRecognizePlant.setOnClickListener(this);
        mDataBinding.ivDeleteRecord.setOnClickListener(this);
        mDataBinding.rvRecognizeRecordList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        recordAdapter=new RecordAdapter();
        mDataBinding.rvRecognizeRecordList.setAdapter(recordAdapter);
        //清空弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivTakeVideo:
                startActivity(CameraActivity.class);
                break;
            case R.id.ivRecognizeCar:
                SelPicActivity.type=0;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivRecognizePlant:
                SelPicActivity.type=1;
                startActivity(SelPicActivity.class);
                break;
            case R.id.ivDeleteRecord:
                if (listShow.size()==0) return;
                myDeleteDialog.show();
                break;
            case R.id.ivDialogDeleteRight:
                listShow.clear();
                AppSpUtils.saveRecordList(listShow);
                getData();
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void getData(){
        listShow.clear();
        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecognizeRecordList.setVisibility(View.VISIBLE);
            mDataBinding.tvSelPicNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecognizeRecordList.setVisibility(View.GONE);
            mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
        }
    }

    //清空弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}